#include "User_App.h"
#include "bsp_port.h"
#include "bsp_PWM.h"
#include "bsp_Timer.h"
#include "bsp_ADC.h"
#include "bsp_PT100.h"
#include "u_Key.h"
#include "bsp_pid_ctrl.h"
#include "multi_button.h"

info_t info;

bsp_TIM_t Ti_RUN_LED;
// bsp_TIM_t Ti_HEAT_CTRL;
bsp_TIM_t Ti_BSP_ADC;
bsp_TIM_t Ti_BSP_ADC_DATA;
bsp_TIM_t Ti_HEAT_PID_Ctrl;
bsp_TIM_t Ti_KEY_TIM;

PWM_t HEAT_CTRL; // 加热控制

PID_t PID_Temp = {
    .Set_Value = 200, // 设定温度
    .kp = 5.00,
    .ki = 0.10,
    .kd = 12.0,
};
static Button btn1;

uint8_t GoalHeatPwm = 50; // 目标加热PWM

void bsp_USART1_Debug(void);
void u_key_Debug(void);
void Ti_RUN_LED_event(void);
void Ti_KEY_TIM_event(void);
// static void Ti_HEAT_CTRL_event(void);
void Ti_HEAT_PID_Ctrl_event(void);
uint8_t read_button_gpio(uint8_t button_id);
void btn1_press_repeat_handler(Button *btn);
void btn1_single_click_handler(Button *btn);
void btn1_double_click_handler(Button *btn);
void btn1_long_press_start_handler(Button *btn);
void btn1_long_press_hold_handler(Button *btn);
void btn1_press_up_handler(Button *btn);
void btn1_press_down_handler(Button *btn);

void User_App(void)
{
#if 0

  // LED_Run_Debug();
  // bsp_Ti_Debug();
  // bsp_PWM_Debug();
  // bsp_USART1_Debug();
  // bsp_Adc_Debug();
  // KEY_Debug();
  // bsp_PT100_Debug();
  u_key_Debug();

#else

  HEAT_CTRL = bsp_PWM_Obj_New(HEAT_CTRL_GPIO, 1, GoalHeatPwm, p_RUN);

  bsp_Adc_Init(&ADC_OBEY);
  Ti_obj_New(&Ti_BSP_ADC, 10, Ti_RUN, Ti_BSP_ADC_event);
  Ti_obj_New(&Ti_BSP_ADC_DATA, 10 * ADC_OBJ_DATA_NUM, Ti_RUN, Ti_BSP_ADC_DATA_event);
  Ti_obj_New(&Ti_HEAT_PID_Ctrl, 3000, Ti_RUN, Ti_HEAT_PID_Ctrl_event);

  Ti_obj_New(&Ti_RUN_LED, 500, Ti_RUN, Ti_RUN_LED_event);
  // Ti_obj_New(&Ti_HEAT_CTRL, 5000, Ti_RUN, Ti_HEAT_CTRL_event);

  while (1)
  {
    bsp_Ti_All_Handle();
    KEY_All_event_Handle();
  }
#endif
}

/*  @brief  加热定时PID控制
    @return 无
    @property 放在定时器中
*/
void Ti_HEAT_PID_Ctrl_event(void)
{
  info.i_NowTemp = bsp_Get_Temp_Data();

  if (info.i_NowTemp < PID_Temp.Set_Value - 40)
  {                                     // 强制加热
    bsp_PWM_Set_Pulse(&HEAT_CTRL, 100); // 加热器2 设置PWM占空比
    bsp_PWM_Set_Run(&HEAT_CTRL);
    PID_Temp.Total_Error = 0;
    PID_Temp.Pre_Error = 0;
    PID_Temp.Now_Value = info.i_NowTemp; // 传入实际值
  }
  else
  {
    bsp_PID_Control(&PID_Temp, PID_Temp.Set_Value, info.i_NowTemp); // 将PID的数值传入
    if (PID_Temp.PidOut < 0)
    {
      if (PID_Temp.Total_Error < -240)
      {
        PID_Temp.Total_Error = 0;
      }
      if (PID_Temp.PidOut < 0)
        PID_Temp.PidOut = 0;
      bsp_PWM_Set_Stop(&HEAT_CTRL);
    }
    else
    {
      if (PID_Temp.PidOut > 100)
        PID_Temp.PidOut = 100;
      bsp_PWM_Set_Pulse(&HEAT_CTRL, (uint8_t)PID_Temp.PidOut); // 加热器2 设置PWM占空比
      bsp_PWM_Set_Run(&HEAT_CTRL);
    }
  }
  uint16_t Set_Value_OUT = PID_Temp.Set_Value * 100.0;
  uint16_t i_NowTemp_OUT = info.i_NowTemp * 100.0;

  uint8_t buff01[7] = {0};
  buff01[0] = 0xAA;
  buff01[1] = 0x01;
  buff01[2] = (uint16_t)Set_Value_OUT >> 8; // 设定温度
  buff01[3] = (uint16_t)Set_Value_OUT & 0xff;
  buff01[4] = (uint16_t)i_NowTemp_OUT >> 8; // 实际温度
  buff01[5] = (uint16_t)i_NowTemp_OUT & 0xff;
  buff01[6] = 0xBB;

  bsp_USART_SendBytes(buff01, 7);
}
void Ti_KEY_TIM_event(void)
{
  button_ticks();
}
void LED_Run_Debug(void)
{
  while (1)
  {
    LED_RUN_TOGG;
    LL_mDelay(500);
  }
}
// 按键调试
void u_key_Debug(void)
{
  // 初始化按键 (active_level: 0=低电平有效, 1=高电平有效)
  button_init(&btn1, read_button_gpio, 0, 1);
  button_attach(&btn1, BTN_PRESS_DOWN, btn1_press_down_handler);             // 按下事件
  button_attach(&btn1, BTN_PRESS_UP, btn1_press_up_handler);                 // 抬起事件
  button_attach(&btn1, BTN_PRESS_REPEAT, btn1_press_repeat_handler);         // 重复按压
  button_attach(&btn1, BTN_SINGLE_CLICK, btn1_single_click_handler);         // 单击
  button_attach(&btn1, BTN_DOUBLE_CLICK, btn1_double_click_handler);         // 双击
  button_attach(&btn1, BTN_LONG_PRESS_START, btn1_long_press_start_handler); // 长按
  button_attach(&btn1, BTN_LONG_PRESS_HOLD, btn1_long_press_hold_handler);   // 长按保持
  button_start(&btn1);
  Ti_obj_New(&Ti_KEY_TIM, 5, Ti_RUN, Ti_KEY_TIM_event);

  while (1)
  {
    bsp_Ti_All_Handle();
  }
}
void bsp_USART1_Debug(void)
{
  while (1)
  {
    printf("------>\n");
    LL_mDelay(500);
  }
}
void Ti_RUN_LED_event(void)
{
  // 添加需要执行的程序
  // printf("hedddd\n");

  LED_RUN_TOGG;
}
#if 0
static void Ti_HEAT_CTRL_event(void)
{
  // 添加需要执行的程序
  // printf("hedddd\n");
  // LED_RUN_TOGG;

  switch (GoalHeatPwm)
  {
  case 50:
    GoalHeatPwm = 100;
    bsp_PWM_Set_Pulse(&HEAT_CTRL, GoalHeatPwm);
    Ti_obj_Set_Cycle(&Ti_HEAT_CTRL, 35000);
    break;
  case 100:
    GoalHeatPwm = 50;
    bsp_PWM_Set_Pulse(&HEAT_CTRL, GoalHeatPwm);
    Ti_obj_Set_Stop(&Ti_HEAT_CTRL);
    LED_GEAR_03_OPEN;
    break;

  default:
    break;
  }
}
#endif

uint8_t read_button_gpio(uint8_t button_id)
{
  switch (button_id)
  {
  case 1:
    return LL_GPIO_IsInputPinSet(KEY_01_GPIO_Port, KEY_01_Pin);
  default:
    return 0;
  }
}
void btn1_press_down_handler(Button *btn)
{
  printf("Button 1:按下事件\n");
}
void btn1_press_up_handler(Button *btn)
{
  printf("Button 1:抬起事件\n");
}
void btn1_press_repeat_handler(Button *btn)
{
  printf("Button 1: 重复按压\n");
}
void btn1_single_click_handler(Button *btn)
{
  printf("Button 1: 单击完整\n");
}
void btn1_double_click_handler(Button *btn)
{
  printf("Button 1: 双击完整\n");
}
void btn1_long_press_start_handler(Button *btn)
{
  printf("Button 1: 长按\n");
}
void btn1_long_press_hold_handler(Button *btn)
{
  printf("Button 1: 长按保持\n");
}
